package com.pdf.model;

import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.Iterator;

import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.AcroFields;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;

public class PDFModelFill {

    public static void main(String[] args){
        PdfStamper stamp = null;
        try{
            String filePath = "E:/";
            PdfReader reader = new PdfReader(filePath + "/temp.pdf");
            stamp = new PdfStamper(reader,new FileOutputStream(filePath + "/result.pdf"));
            
            PdfContentByte under = stamp.getUnderContent(1);/**使用中文字体 */
            BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
            Font fontChinese = new Font(bf,20,Font.NORMAL);
            
            Paragraph paragraph = new Paragraph("你好",fontChinese);
            
            AcroFields fields = stamp.getAcroFields();
            
            for(Iterator it = fields.getFields().keySet().iterator(); it.hasNext();){
                System.out.println(it.next());
            }
            
            fields.setField("COM_NAME", "公司名称");
            /** 必须设置的值？*/
            fields.setField("REPORT_ID", "QIP-ASI106203");
            
            stamp.setFormFlattening(true);
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            if (stamp != null) {
                try {
                    stamp.close();
                }
                catch (DocumentException e) {
                    e.printStackTrace();
                }
                catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
